const app = getApp();
Page({
  data: {
    levelList:{},
    myLevel:'',
    api:app.globalData.api,
    token:''
  },
  onLoad() {
    let that = this;
    my.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          token:res.data?res.data.token:'',
        })
        that.getMyLevelData();
        that.getLevelRule()
      }
    });
  },
  getMyLevelData(){
    let that = this;
    my.request({
      url:this.data.api+'/user/myLevel',
      method: 'get',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.errCode==401){//token过期了
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            token:''
          })
          my.showToast({
            type: 'exception',
            content: res.data.message,
            duration: 2000
          });
          my.reLaunch({ url: '../login/login' });
          return false
        }
        if(res.data.errCode==0){
          that.setData({
            myLevel:res.data.level+'级分销商'
          })
        }
      }
    });
  },
  getLevelRule(){
    let that = this;
    my.request({
      url:this.data.api+'/commission/rule',
      method: 'get',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(res.data.errCode==0){
          let list = res.data.result.map((val,index,arr)=>{
            return {
              grade:val.level,
              order:val.order_num,
              proportion:val.proportion
            }
          })
          that.setData({
            levelList:{
              tableThemes:{'grade':'等级','order':'订单数量','proportion':'佣金比例'},
              tableItems:list
            },
          })
        }
      }
    });
  }
});
